<html><head><title>readcsv.awk</title>
<link rel="stylesheet" href="base.css">
<link rel="stylesheet" href="print.css"  media="print">
<link rel="stylesheet" href="screen.css" media="screen">
</head>
<body>
<pre><code> @include <b><font color=teal>"lib.awk"</font></b> # standard utils

 <b><font color=brown>BEGIN</font></b> <b>{</b> FS=<b><font color=teal>","</font></b> ; FindClass=<b><font color=teal>"theClass"</font></b><b>}</b>

 <b><font color=brown>function</font></b> readcsv(name,lo,hi,goals,d,w,has,csv,logp,  r) <b>{</b>
   csv = csv ? csv : <b><font color=teal>"cat -"</font></b>  
   <b><font color=brown>while</font></b>((csv | getline ) &gt; 0) <b>{</b>   # <b><font color=brown>for</font></b> every line do
     <b><font color=brown>gsub</font></b>(/[ \t\r]/,<b><font color=teal>""</font></b>)            # kill whitespace
     <b><font color=brown>gsub</font></b>(/(#.*|[ \t'<b><font color=teal>"])/,"</font></b>")      # kill comments
     <b><font color=brown>if</font></b> (/^$/) <b><font color=brown>continue</font></b>            # skip empty lines
     <b><font color=brown>if</font></b> (r) # then the header is read and we can process the data
     readline(name,lo,hi,goals,w,has,d,r,logp) 
     <b><font color=brown>else</font></b> # we have seen no lines yet; time to read the header
     readhead(name,lo,hi,goals,w) 
     r++  <b>}</b>                                                 
   close(csv)
 <b>}</b>
 <b><font color=brown>function</font></b> readhead(name,lo,hi,goals,w,   c) <b>{</b>
   <b><font color=brown>for</font></b>(c=1;c&lt;=NF;c++) <b>{</b>
     <b><font color=brown>if</font></b> ($c ~ /\?/) <b><font color=brown>continue</font></b> # skip the stuff we are ignoring                   
     w[c] = weight($c)       # record feature weights
     <b><font color=brown>if</font></b> ($c ~ /!/) goals[c]  # record <b><font color=brown>if</font></b> this is a goal        
     <b><font color=brown>if</font></b> ($c ~ /\$/)          # <b><font color=brown>if</font></b> this a numeric feature, then...
       hi[c]= -1 * (lo[c] = 10^10) # ... initialize the hi and lo values
     <b><font color=brown>gsub</font></b>(/\*.*/,<b><font color=teal>""</font></b>,$c)      # wipe the weight info
     name[c] = $c            # record the name             

 <b>}</b><b>}</b>
 <b><font color=brown>function</font></b> weight(colname,   n,tmp) <b>{</b>
   n = split(colname,tmp,/\*/)    # does <b><font color=teal>"name"</font></b> look like <b><font color=teal>"name*n"</font></b> ?
   <b><font color=brown>return</font></b> n==1 ? 1 : tmp[2]*1     # <b><font color=brown>if</font></b> not, weight = 1. <b><font color=brown>else</font></b>, weight = n
 <b>}</b>
 <b><font color=brown>function</font></b> readline(name,lo,hi,goals,w,has,d,r,logp,   \
                   val,class,c) <b>{</b>
   class = @FindClass(goals) # summarize goals to one <b><font color=teal>"class"</font></b> value
   <b><font color=brown>for</font></b>(c <b><font color=brown>in</font></b> w) <b>{</b> 
     val = $c
     <b><font color=brown>if</font></b> (logp &amp;&amp; c <b><font color=brown>in</font></b> lo &amp;&amp; val != <b><font color=teal>"?"</font></b>)  # maybe log numerics
       val = log(val &lt; 0.0001 ? 0.0001 : val)
     d[r][c] = val      # store data
     <b><font color=brown>if</font></b> (val !~ /?/) <b>{</b>  # <b><font color=brown>if</font></b> we aren't ignoring this cell...
       has[c][val][class]++   
       <b><font color=brown>if</font></b> (c <b><font color=brown>in</font></b> lo) <b>{</b>   # update any info on numerics
         <b><font color=brown>if</font></b> (val &gt; hi[c]) hi[c] = val
         <b><font color=brown>if</font></b> (val &lt; lo[c]) lo[c] = val <b>}</b><b>}</b><b>}</b>
 <b>}</b>
 <b><font color=brown>function</font></b> theClass(goals,r,d,    c) <b>{</b> 
   <b><font color=brown>if</font></b> (r) <b><font color=brown>for</font></b>(c <b><font color=brown>in</font></b> goals) <b><font color=brown>return</font></b> d[r][c] # <b><font color=brown>for</font></b> data from the <b><font color=teal>"d"</font></b> array
   <b><font color=brown>else</font></b>   <b><font color=brown>for</font></b>(c <b><font color=brown>in</font></b> goals) <b><font color=brown>return</font></b> $c      # <b><font color=brown>for</font></b> data from standard input
 <b>}</b>
</code></pre>

<h1 id=<b><font color=teal>"author"</font></b>>Author</h1>

<p>Tim Menzies, tim@menzies.us, http://menzies.us</p>

<h1 id=<b><font color=teal>"copyright"</font></b>>Copyright</h1>

<p>Copyright (c) 2012, Tim Menzies.</p>

<p>Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
<b><font color=teal>"Software"</font></b>), to deal <b><font color=brown>in</font></b> the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:</p>

<p>The above copyright notice and this permission notice shall be
included <b><font color=brown>in</font></b> all copies or substantial portions of the Software.</p>

<p>THE SOFTWARE IS PROVIDED <b><font color=teal>"AS IS"</font></b>, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p>
</body></html>
